import React from 'react';
import {Spin, Card, Button, Table} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import routes from './routes';
import {TABLE, DATETIME_FORMAT} from '../constants';
import {formatDateTime, formatAmount, onDownloadHistory} from '../utils';
import {getHistoryDetail, getHistoryDownload} from './api';
import {injectIntl} from './intl';

const {Description} = DescriptionList;

class HistoryDetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: [],
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getHistoryDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details,
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }

    onCancel = () => {
        this.props.history.push(routes.historyQuery.url());
    }

    onClickDownload = e => {
        const {id, applyid} = e.target.dataset;
        this.setState({
            isFetching: true
        });
        onDownloadHistory({
            filePath: id,
            fileName: applyid
        });
    }
    render() {
        const {data, details} = this.state;
        const {attachments = []} = data;
        const {formatMessage} = this.props.intl;
        const detailColums = [{
            title: formatMessage({
                id: 'historyDetailPanel.column.claimOrder',
                defaultMessage: '索赔单号'
            }),
            dataIndex: 'claimOrder'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.vin',
                defaultMessage: 'VIN'
            }),
            dataIndex: 'vin'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.series',
                defaultMessage: '车系'
            }),
            dataIndex: 'series'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.isMainPart',
                defaultMessage: '是否主损件'
            }),
            dataIndex: 'isMainPart',
            render: (record, text) => record ? formatMessage({
                id: 'historyDetailPanel.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'historyDetailPanel.column.no',
                defaultMessage: '否'
            })
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.quantity',
                defaultMessage: '数量'
            }),
            dataIndex: 'quantity'
        }];
        if(!this.props.isMoneyVisible)
            detailColums.splice(7, 0, {
                title: formatMessage({
                    id: 'historyDetailPanel.column.partPrice',
                    defaultMessage: '单价'
                }),
                dataIndex: 'partPrice',
                render: text => formatAmount(text)
            }, {
                title: formatMessage({
                    id: 'historyDetailPanel.column.partAmountSum',
                    defaultMessage: '备件金额'
                }),
                dataIndex: 'partAmountSum',
                render: text => formatAmount(text)
            }, {
                title: formatMessage({
                    id: 'historyDetailPanel.column.manageFeeSum',
                    defaultMessage: '管理费'
                }),
                dataIndex: 'manageFeeSum',
                render: text => formatAmount(text)
            }, {
                title: formatMessage({
                    id: 'historyDetailPanel.column.laborAmountSum',
                    defaultMessage: '工时金额合计'
                }),
                dataIndex: 'laborAmountSum',
                render: text => formatAmount(text)
            }, {
                title: formatMessage({
                    id: 'historyDetailPanel.column.materialAmountSum',
                    defaultMessage: '辅料金额合计'
                }),
                dataIndex: 'materialAmountSum',
                render: text => formatAmount(text)
            }, {
                title: formatMessage({
                    id: 'historyDetailPanel.column.totalFee',
                    defaultMessage: '总计'
                }),
                dataIndex: 'totalFee',
                render: text => formatAmount(text)
            });
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.claimSettlementHistory',
                            defaultMessage: '索赔结算历史查询详情'
                        })}>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.uploadName',
                                defaultMessage: '上传人'
                            })}>{data.uploadName}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.uploadDate',
                                defaultMessage: '上传时间'
                            })}>{formatDateTime(data.uploadDate, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.piAuditDate',
                                defaultMessage: 'PI审核时间'
                            })}>{formatDateTime(data.piAuditDate, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.piAuditName',
                                defaultMessage: 'PI审核人'
                            })}>{data.piAuditName}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.piAuditRemark',
                                defaultMessage: 'PI审核意见'
                            })}>{data.piAuditRemark}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.details',
                            defaultMessage: '结算清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={detailColums}
                                rowKey="id"
                                pagination={false}
                                dataSource={details}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    {attachments && attachments.length >= 0
                        ? <Card><DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.attachment',
                            defaultMessage: '历史附件'
                        })}>
                            {attachments.map(item => (<Description key={item.id}>
                                <a onClick={this.onClickDownload} key="delete" data-id={item.filePath} data-applyid={item.fileName}>
                                    {item.fileName}</a></Description>))}
                        </DescriptionList></Card>
                        : <Card><DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList></Card>}
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{formatMessage({
                        id: 'historyDetailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

HistoryDetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
    isMoneyVisible: PropTypes.bool
};
import {connect} from 'react-redux';
const mapStateToProps = (state, ownProps) => ({
    isMoneyVisible: state.getIn((['page', 'domainData', 'initData', 'isMoneyVisible'])),
});

export default connect(
    mapStateToProps,
    null
)(injectIntl(HistoryDetailPanel));
